<script>

import {formatTime} from "@/utils/formatTime.js";
import {add, all, del, edit, filterGoods} from "@/api/goods.js";

export default {
  name: "goods",
  data() {
    return {
      goodsData: [],
      editGoodsDialogVisible: false,
      addGoodsDialogVisible: false,
      filterDialogVisible: false,
      editGoodsFormData: {},
      addGoodsFormData: {},
      filterFormData: {},
    }
  },
  methods: {
    handleDelete(id) {
      del(id).then(res => {
        if (res.code === 200) {
          this.$message.success("删除成功")
          this.refresh()
        } else {
          this.$message.error("删除失败")

        }
      })
    },
    formatTime,
    refresh() {
      all().then(res => {
        if (res.code === 200) {
          this.goodsData = res.data
        } else {
          this.$message.error("获取数据失败")
        }
      })
    },
    handleEdit(data) {
      this.editGoodsFormData = data
      this.editGoodsDialogVisible = true
    },
    editGoods(data) {
      edit(data).then(res => {
        if (res.code === 200) {
          this.$message.success("修改成功")
          this.refresh()
          this.editGoodsDialogVisible = false
          this.editGoodsFormData = {}
        } else {
          this.$message.error("修改失败")
        }

      })
    },
    addGoods(data) {
      data.img = JSON.stringify(data.img)
      add(data).then(res => {
        if (res.code === 200) {
          this.$message.success("添加成功")
          this.refresh()
          this.addGoodsDialogVisible = false
          this.addGoodsFormData = {}
        } else {
          this.$message.error("添加失败")
        }
      })
    },
    filters(data) {
      filterGoods(data).then(res => {
        if (res.code === 200) {
          this.goodsData = res.data
          this.filterDialogVisible = false
        } else {
          this.$message.error("获取数据失败")
        }
      })
    }
  },
  created() {
    this.refresh()
  }

}
</script>

<template>
  <div id="goods-title">
    商品管理
  </div>
  <div id="goods-box">
    <div id="goods-form">
      <el-button type="primary" @click="filterDialogVisible = true">增加检索</el-button>
      <el-button type="primary" @click="addGoodsDialogVisible = true">新增数据</el-button>
      <el-button type="primary">导入数据</el-button>
      <el-button type="primary">导出数据</el-button>
      <el-button type="primary" @click="refresh">刷新</el-button>
    </div>

    <div>
      <el-table id="goods-data" :data="goodsData">
        <el-table-column type="selection" width="50px"/>
        <el-table-column label="推荐" prop="tuijian" width="100"></el-table-column>
        <el-table-column label="校区" prop="school" width="100"></el-table-column>
        <el-table-column label="商品名称" prop="name" width="150"></el-table-column>
        <el-table-column label="商品类型" prop="types" width="100"></el-table-column>
        <el-table-column label="价格" prop="price" width="100"></el-table-column>
        <el-table-column label="数量" prop="num" width="100"></el-table-column>
        <el-table-column label="图片" prop="img" width="200">
          <template #default="scope">
            <img :src="scope.row.img" style="width: 100px; height: 100px;" alt="图片">
          </template>
        </el-table-column>
        <el-table-column label="创建时间" prop="createTime" width="150">
          <template #default="scope">
            <span>{{ formatTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="修改时间" prop="updateTime" width="150">
          <template #default="scope">
            <span>{{ formatTime(scope.row.updateTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="300">
          <template #default="scope">
            <el-button @click="handleEdit(scope.row)" type="primary">编辑</el-button>
            <el-button @click="handleDelete(scope.row.id)" type="danger">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

  </div>
  <!-- 修改表单 -->
  <div>
    <el-dialog title="修改商品" v-model="editGoodsDialogVisible" :close-on-click-modal='false'>
      <el-form :model="editGoodsFormData">
        <el-form-item label="校区">
          <el-input v-model="editGoodsFormData.school"></el-input>
        </el-form-item>
        <el-form-item label="商品类型">
          <el-input v-model="editGoodsFormData.types"></el-input>
        </el-form-item>
        <el-form-item label="商品名称">
          <el-input v-model="editGoodsFormData.name"></el-input>
        </el-form-item>
        <el-form-item label="价格">
          <el-input v-model="editGoodsFormData.price"></el-input>
        </el-form-item>
        <el-form-item label="数量">
          <el-input v-model="editGoodsFormData.num"></el-input>
        </el-form-item>
        <el-form-item label="是否推荐">
          <el-input v-model="editGoodsFormData.tuijian"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="info" @click="editGoodsDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="editGoods(editGoodsFormData)">提交</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>

  <!-- 添加表单 -->
  <div>
    <el-dialog title="新增商品" v-model="addGoodsDialogVisible" :close-on-click-modal='false'>
      <el-form :model="addGoodsFormData">
        <el-form-item label="校区">
          <el-input v-model="addGoodsFormData.school"></el-input>
        </el-form-item>
        <el-form-item label="商品类型">
          <el-input v-model="addGoodsFormData.types"></el-input>
        </el-form-item>
        <el-form-item label="商品名称">
          <el-input v-model="addGoodsFormData.name"></el-input>
        </el-form-item>
        <el-form-item label="价格">
          <el-input v-model="addGoodsFormData.price"></el-input>
        </el-form-item>
        <el-form-item label="数量">
          <el-input v-model="addGoodsFormData.num"></el-input>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="addGoodsFormData.phone"></el-input>
        </el-form-item>
        <el-form-item label="商品描述">
          <el-input v-model="addGoodsFormData.content"></el-input>
        </el-form-item>
        <el-form-item label="商品图片">
          <el-input v-model="addGoodsFormData.img"></el-input>
        </el-form-item>
        <el-form-item label="用户编号">
          <el-input v-model="addGoodsFormData.userId"></el-input>
        </el-form-item>
        <el-form-item label="是否推荐">
          <el-input v-model="addGoodsFormData.tuijian"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="info" @click="addGoodsDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="addGoods(addGoodsFormData)">提交</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
  <!-- 过滤表单 -->
  <div>
    <el-dialog :model-value="filterDialogVisible" title="过滤表单" :close-on-click-modal='false'>
      <el-form>
        <el-form-item label="是否推荐">
          <el-select v-model="filterFormData.tuijian" placeholder="请选择问题类型">
            <el-option label="全部" value=""></el-option>
            <el-option label="推荐" value="true"></el-option>
            <el-option label="不推荐" value="false"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="校区">
          <el-select v-model="filterFormData.school" placeholder="请选择问题类型">
            <el-option label="全部" value=""></el-option>
            <el-option label="奉贤校区" value="奉贤校区"></el-option>
            <el-option label="徐汇校区" value="徐汇校区"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品类型">
          <el-select v-model="filterFormData.type" placeholder="请选择问题类型">
            <el-option label="全部" value=""></el-option>
            <el-option label="学习资料" value="学习资料"></el-option>
            <el-option label="课外书" value="课外书"></el-option>
            <el-option label="电子产品" value="电子产品"></el-option>
            <el-option label="日用品" value="日用品"></el-option>
            <el-option label="衣服" value="衣服"></el-option>
            <el-option label="运动" value="运动"></el-option>
            <el-option label="美妆" value="美妆"></el-option>
            <el-option label="鞋靴" value="鞋靴"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <el-form-item>
        <el-button type="info" @click="filterDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="filters(filterFormData)">搜索</el-button>
      </el-form-item>
    </el-dialog>
  </div>
</template>

<style scoped>
#goods-box {
  background-color: #68a273;
  width: 90%;
  height: 90%;
  margin-top: 2%;
  margin-left: 3%;
  border-radius: 30px;
}

#goods-title {
  font-size: 30px;
  margin-left: 20px;
}

#goods-form {
  display: flex;
  padding-top: 40px;
  justify-content: flex-end;
  margin-right: 80px;
  margin-bottom: 10px;
}
#goods-data{
  width: 100%;
  align-items: center;
}
</style>